package org.hwadee.service;

import org.hwadee.entity.Interest;
import org.hwadee.mapper.InterestMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class InterestService {

    @Autowired
    private UserService userService;

    @Autowired
    private InterestMapper interestMapper;

    public List<Interest> getAllFollower(Integer userId) {
        List<Interest> interests = interestMapper.getAllFollower(userId);
        for(Interest interest : interests) {
            interest.setFollowerUser(userService.getUserById(interest.getFollowerId()));
            interest.setFollowedUser(userService.getUserById(interest.getFollowedId()));
            if(this.checkInterest(interest.getFollowerId(),interest.getFollowedId())==1 &&this.checkInterest(interest.getFollowedId(),interest.getFollowerId())==1) {
                interest.setIsDouble(1);
            }

        }
        return interests;
    }

    public List<Interest> getAllFollowed(Integer userId) {
        List<Interest> interests = interestMapper.getAllFollowed(userId);
        for(Interest interest : interests) {
            interest.setFollowerUser(userService.getUserById(interest.getFollowerId()));
            interest.setFollowedUser(userService.getUserById(interest.getFollowedId()));
            if(this.checkInterest(interest.getFollowerId(),interest.getFollowedId())==1 &&this.checkInterest(interest.getFollowedId(),interest.getFollowerId())==1) {
                interest.setIsDouble(1);
            }
        }
        return interests;
    }

    /**
     *
     * @param followerId
     * @param followedId
     * @return
     */
    public int checkInterest(Integer followerId, Integer followedId) {
        return interestMapper.checkInterest(followerId,followedId);
    }

    public void insertInterest(Interest interest) {
        interestMapper.insertInterest(interest);
    }

    public void deleteInterest(Interest interest) {
        interestMapper.deleteInterest(interest);
    }

//    public void addFollowerCount(Integer interestId) { interestMapper.addFollowerCount(interestId); }
//
//    public void addFollowedCount(Integer interestId) { interestMapper.addFollowedCount(interestId); }
//
//    public void reduceFollowerCount(Integer interestId) { interestMapper.reduceFollowerCount(interestId); }
//
//    public void reduceFollowedCount(Integer interestId) { interestMapper.reduceFollowedCount(interestId); }

}
